#!/usr/bin/env python
# -*- coding: utf-8 -*-
import timeit
from datetime import datetime
from datetime import timedelta, date
import os.path
from string import zfill
 
weather_dict = {}
type_dict = {}

def dict_to_str(p_dict):
    arr = [': '.join([str(k), str(v)]) for k, v in p_dict.iteritems()]
    return ', '.join(i for i in arr)

def format_date(str_date):#
    return datetime.strptime(str_date, '%Y%m%d')

def format_holiday(obj_date):
    arr_holiday = [datetime(2014, 9, 8), 
                   datetime(2014, 10, 1), datetime(2014, 10, 2), datetime(2014, 10, 3),
                   datetime(2014, 10, 4), datetime(2014, 10, 5), datetime(2014, 10, 6),
                   datetime(2014, 10, 7)]
    arr_not_holiday = [datetime(2014, 9, 28), datetime(2014, 10, 11)]
    arr_weekend = [5, 6] #Saturday and Sunday
    if (obj_date.weekday() not in arr_weekend \
        and obj_date not in arr_holiday) \
        or obj_date in arr_not_holiday:
        return '0'
    else:
        return '1'
    
def init_type():
    global type_dict
    print 'init type...'
    input_file = '/home/ivy/git/passenger/passenger/resource/type.txt'
    counter = 1;
    with open(input_file, 'r') as f:
        for line in f:
            type = line.strip()
            type_dict[type] = str(counter)
            counter += 1
    f.close()
    print 'type_dict: %s' % dict_to_str(type_dict)


def init_weather():
    global weather_dict
    print 'init weather...'
    input_file = '/home/ivy/git/passenger/passenger/resource/weather.txt'
    counter = 1;
    with open(input_file, 'r') as f:
        for line in f:
            weather = line.strip()
            weather_dict[weather] = str(counter)
            counter += 1
    f.close()
    print 'weather_dict: %s' % dict_to_str(weather_dict)

def format_by_day(line = 10):
    input_file = '/home/ivy/git/passenger/passenger/resource/result_day_%d.txt' % line
    output_file = 'format/day.txt'
    opf = open(output_file, 'w+')
    with open(input_file, 'r') as f:
        #10, 20140801, 44256, 晴, 36, 雷阵雨, 26
        for line in f:
            arr = [i.strip() for i in line.strip().split(',')]
            str_date = arr[1]
            weekday = str(format_date(str_date).weekday()+1)
            holiday = format_holiday(format_date(str_date))
            weather_day = weather_dict[arr[3]]
            weather_night = weather_dict[arr[5]]
            opf.write('\t'.join([str_date, weekday, holiday, weather_day, arr[4], weather_night, arr[6], arr[2]]) + '\n')
    opf.flush()
    opf.close()
    f.close()
    
def format_by_day_type(line = 10):
    input_file = '/home/ivy/git/passenger/passenger/resource/result_day_type_%d.txt' % line
    output_file = '/home/ivy/git/passenger/passenger/resource/format/day_type_%d.txt' % line
    opf = open(output_file, 'w+')
    with open(input_file, 'r') as f:
        #10, 20140801, 员工卡, 309, 晴, 36, 雷阵雨, 26
        for line in f:
            arr = [i.strip() for i in line.strip().split(',')]
            str_date = arr[1]
            holiday = format_holiday(format_date(str_date))
            type = type_dict[arr[2]]
            weather_day = weather_dict[arr[4]]
            weather_night = weather_dict[arr[6]]
            opf.write('\t'.join([holiday, type, weather_day, arr[5], weather_night, arr[7], arr[3]]) + '\n')
    opf.flush()
    opf.close()
    f.close()
    
def format_by_day_type2(line = 10):
    input_file = '/home/ivy/git/passenger/passenger/resource/result_day_type_%d.txt' % line
    output_file = 'format/day_type.txt'
    opf = open(output_file, 'w+')
    opf.write('date\tweekday\tholiday\ttype\tweather_day\ttemp_day\tweather_night\tpassengers\n')
    
    current_date = ''
    has_type_dict = {}
    info_arr = []
    with open(input_file, 'r') as f:
        #10, 20140801, 员工卡, 309, 晴, 36, 雷阵雨, 26
        for line in f:
            arr = [i.strip() for i in line.strip().split(',')]
            str_date = arr[1]
            if current_date != str_date and current_date != '':
                weekday = str(format_date(info_arr[1]).weekday()+1)
                holiday = format_holiday(format_date(info_arr[1]))
                weather_day = weather_dict[info_arr[4]]
                weather_night = weather_dict[info_arr[6]]
                for k, v in type_dict.iteritems():
                    if k in has_type_dict:
                        opf.write('\t'.join([current_date, weekday, holiday, v, weather_day, info_arr[5], \
                                             weather_night, info_arr[7], has_type_dict[k]]) + '\n')
                    else:
                        opf.write('\t'.join([current_date, weekday, holiday, v, weather_day, info_arr[5], \
                                             weather_night, info_arr[7], '0']) + '\n')
                has_type_dict.clear()
            current_date = str_date
            info_arr = arr
            has_type_dict[arr[2]] = arr[3]
    #last day
    weekday = str(format_date(info_arr[1]).weekday()+1)
    holiday = format_holiday(format_date(info_arr[1]))
    weather_day = weather_dict[info_arr[4]]
    weather_night = weather_dict[info_arr[6]]
    for k, v in type_dict.iteritems():
        if k in has_type_dict:
            opf.write('\t'.join([current_date, weekday, holiday, v, weather_day, info_arr[5], \
                                 weather_night, info_arr[7], has_type_dict[k]]) + '\n')
        else:
            opf.write('\t'.join([current_date, weekday, holiday, v, weather_day, info_arr[5], \
                                 weather_night, info_arr[7], '0']) + '\n')
            
    opf.flush()
    opf.close()
    f.close()
    
    
def format_by_hour(line = 10):
    input_file = '/home/ivy/git/passenger/passenger/resource/result_hour.txt'
    date_dict = {}
    date_info_dict = {}
    with open(input_file, 'r') as f:
        for line in f:
            #10, 2014080100, 86, 晴, 36, 雷阵雨, 26
            arr = line.strip().split(', ')
            bus = arr[0]
            if bus == '15':
                break
            date_str = arr[1][:8]
            hour_str = int(arr[1][-2:])
            num = arr[2]
            weekday = str(format_date(date_str).weekday()+1)
            holiday = format_holiday(format_date(date_str))
            weather_day = weather_dict[arr[3]]
            weather_night = weather_dict[arr[5]]
            if not date_info_dict.has_key(date_str):
                date_info_dict[date_str] = [weekday, holiday, weather_day, arr[4], weather_night, arr[6]]
            if not date_dict.has_key(date_str):
                date_dict[date_str] = {}
            date_dict[date_str][hour_str] = [num]
    f.close()
    print len(date_dict)
    
    output_file = 'format/hour.txt'
    opf = open(output_file, 'w+')
    opf.write('date\thour\tweekday\tholiday\tweather_day\ttemp_day\tweather_night\tpassengers\n')

    start_date = date(2014, 8, 1)
    end_date = date(2015, 1, 1)
    for single_date in daterange(start_date, end_date):
        date_str = single_date.strftime("%Y%m%d")
        num = ['0']
        for i in range(5, 23):
#             hour_str = str(i).zfill(2)
            hour_str = i
            if date_dict.has_key(date_str):
                if date_dict[date_str].has_key(hour_str):
                    num = date_dict[date_str][hour_str]
#                     opf.write('\t'.join([date_str, str(hour_str)]+date_info_dict[date_str]+num) + '\n')
                    if i < 18:
                        opf.write('\t'.join([date_str, str(hour_str)]+date_info_dict[date_str][0:4]+num) + '\n')
                    else:
                        opf.write('\t'.join([date_str, str(hour_str)]+date_info_dict[date_str][0:2]\
                                            + date_info_dict[date_str][-2:]+num) + '\n')
#             if date_info_dict.has_key(date_str):
#                 opf.write('\t'.join([date_str, hour_str]+date_info_dict[date_str]+num) + '\n')
#             else:
#                 opf.write('\t'.join([date_str, hour_str]+['2', '0', '5', '33', '5', '25']+num) + '\n')
                
def daterange(start_date, end_date):
    for n in range(int ((end_date - start_date).days)):
        yield start_date + timedelta(n)
        
        
def format_hour_type():
    hour_file = os.path.join('format','hour.txt')
    hour_type_file = '/home/ivy/git/passenger/passenger/resource/result_hour_type.txt'
#     htf = open(hour_type_file, 'r')
    output_file = os.path.join('format', 'hour_type.txt')
    opf = open(output_file, 'w+')
    
    hour_dict = {}
    with open(hour_file, 'r') as f:
        f.readline()
        #20140801    5    5    0    1    36    37
        for line in f:
            arr = line.strip().split('\t')
            key = arr[0]+arr[1].zfill(2)
            info = arr[2:6]
            hour_dict[key] = info 
    f.close()
    
    current_time = ''
    has_type = []
    with open(hour_type_file, 'r') as f:
        for line in f:
            #10, 2014080100, 学生卡, 2, 晴, 36, 雷阵雨, 26
            arr = line.strip().split(', ')
            hour = int(arr[1][-2:])
            if arr[0] != '10':
                break
            if hour < 5 or hour > 22:
                continue
            if current_time != '' and current_time != arr[1]:
                for k, v in type_dict.iteritems():
                    if v not in has_type:
                        opf.write('\t'.join([current_time[:8], str(current_hour), v]+hour_dict[current_time]+['0'])+'\n')
                has_type = []
            current_time = arr[1]
            current_hour = int(current_time[-2:])
            type = type_dict[arr[2]]
            opf.write('\t'.join([current_time[:8], str(hour), type]+hour_dict[arr[1]]+[arr[3]])+'\n')
            has_type.append(type)
    for k, v in type_dict.iteritems():
        if k not in has_type:
            opf.write('\t'.join([current_time[:8], str(current_hour), v]+hour_dict[current_time]+['0'])+'\n')
    opf.flush()
    opf.close()
    f.close()
                      
def main():
#     init_weather()
    init_type()
#     format_by_day(line=10)
#     format_by_day_type2(line=10)
#     format_by_hour()
    format_hour_type()
    
    

if __name__ == '__main__':
    start = timeit.default_timer()
    
    main()
    
    stop = timeit.default_timer()
    print 'run time: %.10fs' % (stop - start)
    
